package cn.rongdao.jrkabao.ui.loan;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chanven.lib.cptr.PtrClassicDefaultHeader;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.bean.loan.MultiPushBean;
import cn.rongdao.jrkabao.model.loan.MultiPushModel;
import cn.rongdao.jrkabao.ui.loan.adapter.MultiPushAdapter;
import cn.rongdao.jrkabao.ui.loan.adapter.listener.MutliPustitemClickListener;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;


/**
 * Created by jiangyongxing on 2017/8/18.
 * 描述：
 */

public class MultiPusuListActivity extends BaseAutoFinishActivity implements MutliPustitemClickListener {

    public static final String CLASSIFY_MULTI_PUSH = "1";//多推送
    public static final String CLASSIFY_REAPPLY = "2";//点我拿钱

    private TitleBarUIComponent mTitleBarUIComponent;
    protected PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    private String mCateId;
    private String mClassify;
    private String mLoanName;
    private MultiPushAdapter mMultiPushAdapter;
    private boolean itemClickStatus = false;

    public static void start(Context context, String cateId, String classify, String loanName) {
        Bundle bundle = new Bundle();
        bundle.putString(InfoConstant.CATE_ID, cateId);
        bundle.putString("classify", classify);
        bundle.putString(InfoConstant.LOAN_NAME, loanName);
        FRStartActivity.start(context, MultiPusuListActivity.class, bundle);
    }


    @Override
    protected int layoutRes() {
        return R.layout.activity_multi_push_list;
    }

    @Override
    protected void initView() {
        initData();
        mTitleBarUIComponent = (TitleBarUIComponent) findViewById(R.id.ampl_title_bar);
        initTitleBar();
        ptrRecyclerViewUIComponent = (PtrRecyclerViewUIComponent) findViewById(R.id.ampl_ptr_framelayout);
        ptrRecyclerViewUIComponent.setRecyclerViewLayoutParmas(0, FRDisplayMetrics.dp2px(this, 10), 0, 0, false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        ptrRecyclerViewUIComponent.setLayoutManager(linearLayoutManager);
        mMultiPushAdapter = new MultiPushAdapter(this, this);
        ptrRecyclerViewUIComponent.setAdapter(mMultiPushAdapter);
        ptrRecyclerViewUIComponent.setBackground(R.color.layout_bg);
        ptrRecyclerViewUIComponent.delayRefresh(100);
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getData();
            }
        });
        ptrRecyclerViewUIComponent.setHeaderType(PtrClassicDefaultHeader.HEADER_TYPE_ANIM);
    }

    private void initTitleBar() {
//        mTitleBarUIComponent.setBottomViewVisiable(true);
        mTitleBarUIComponent.initTitleBlack(mLoanName);
        mTitleBarUIComponent.setBackgroundColor(Color.WHITE);
        mTitleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitleBarUIComponent.initRightHelpCenterBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startHelpCenter();
            }
        });
    }

    private void initData() {
        mCateId = getIntent().getStringExtra(InfoConstant.CATE_ID);
        mClassify = getIntent().getStringExtra("classify");
        mLoanName = getIntent().getStringExtra(InfoConstant.LOAN_NAME);
    }

    private void getData() {
        MultiPushModel.getInstance().execute(mCateId, mClassify, new FRBaseObserver<List<MultiPushBean>>(this) {
            @Override
            protected void onBaseNext(List<MultiPushBean> data) {
                for (MultiPushBean multiPushBean : data) {
                    for (int i = 0; i < multiPushBean.getProduct_list().size() - 1; i++) {
                        multiPushBean.getProduct_list().get(i).setShowCenterShaow(true);
                    }
                }
                mMultiPushAdapter.setDataList(data);
                ptrRecyclerViewUIComponent.refreshComplete();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                ptrRecyclerViewUIComponent.refreshComplete();

            }
        });
    }

    private void startWebViewActivity(String url) {
        WebViewActivity.start(this, url);
    }


    @Override
    public void setItemNotAppliedClickListener(MultiPushBean.ProductListBean productListBean) {
        LoanCommitActivity.startWithOtherJump(this, productListBean.getCate_id(), productListBean.getProduct_id(), InfoConstant.PAGE_SOURCE_MULTI_PUSH, productListBean.getOrg_name());
    }


    @Override
    public void setItemCompletedAndReapplyClickListener(MultiPushBean.ProductListBean productListBean) {
        LoanCommitActivity.startWithOtherJump(this, productListBean.getCate_id(), productListBean.getProduct_id(), InfoConstant.PAGE_SOURCE_MULTI_PUSH, productListBean.getOrg_name());
    }

    @Override
    public void setItemOrderDetailClickListener(String url) {
        startWebViewActivity(url);
    }

    @Override
    public void setItemOrderRepayMentDetailClickListener(String url) {
        startWebViewActivity(url);
    }

    @Override
    public void setItemOrderRepayWayClickListener(String url) {
        startWebViewActivity(url);
    }

//    private void itemLoanClickAndJumpByRequestData(final MultiPushBean.ProductListBean productListBean) {
//        if (itemClickStatus)
//            return;
//        itemClickStatus = true;
//        GetLoanCreditModel.getInstance().execute(productListBean.getProduct_id(), new FRBaseObserver<UserImportDataStatusBean>(this) {
//            @Override
//            protected void onBaseNext(UserImportDataStatusBean data) {
//                ActivityStartUtil.startActivityByJudgeUserInfo(getActivity(), productListBean.getCate_id(), data, productListBean.getProduct_id(), productListBean.getOrg_name(),InfoConstant.PAGE_SOURCE_MULTI_PUSH);
//                itemClickStatus = false;
//            }
//
//            @Override
//            protected void onBaseError(Throwable t) {
//                itemClickStatus = false;
//            }
//        });
//    }


}
